package com.zhss.eshop.wms.service.Impl;

import com.zhss.eshop.common.util.ObjectUtils;
import com.zhss.eshop.wms.constant.PurchaseInputOrderApproveResult;
import com.zhss.eshop.wms.constant.PurchaseInputOrderStatus;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderItemDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderPutOnItemDTO;
import com.zhss.eshop.wms.domain.model.WmsPurchaseInputOrder;
import com.zhss.eshop.wms.mapper.WmsPurchaseInputOrderItemMapper;
import com.zhss.eshop.wms.mapper.WmsPurchaseInputOrderMapper;
import com.zhss.eshop.wms.mapper.WmsPurchaseInputOrderPutOnItemMapper;
import com.zhss.eshop.wms.service.WmsPurchaseInputOrderService;
import com.zhss.eshop.wms.service.handler.PurchaseInputOrderHandler;
import com.zhss.eshop.wms.service.handler.PurchaseInputOrderHandlerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WmsPurchaseInputOrderServiceImpl implements WmsPurchaseInputOrderService{

    @Resource
    private WmsPurchaseInputOrderMapper wmsPurchaseInputOrderMapper;
    /**
     * 采购入库单管理DAO组件
     */
    @Autowired
    private WmsPurchaseInputOrderMapper purchaseInputOrderDAO;
    /**
     * 采购入库单条目管理DAO组件
     */
    @Autowired
    private WmsPurchaseInputOrderItemMapper purchaseInputOrderItemDAO;
    /**
     * 采购入库单上架条目管理的DAO组件
     */
    @Autowired
    private WmsPurchaseInputOrderPutOnItemMapper purchaseInputOrderPutOnItemDAO;
    /**
     * 采购入库单handler工厂
     */
    @Autowired
    private PurchaseInputOrderHandlerFactory handlerFactory;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return wmsPurchaseInputOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(WmsPurchaseInputOrder record) {
        return wmsPurchaseInputOrderMapper.insertSelective(record);
    }

    @Override
    public WmsPurchaseInputOrder selectByPrimaryKey(Long id) {
        return wmsPurchaseInputOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WmsPurchaseInputOrder record) {
        return wmsPurchaseInputOrderMapper.updateByPrimaryKeySelective(record);
    }
    /**
     * 对采购入库单提交审核
     * @param id 采购入库单id
     * @throws Exception
     */
    @Override
    public void submitApprove(Long id) throws Exception {
        WmsPurchaseInputOrder wmsPurchaseInputOrder = purchaseInputOrderDAO.selectByPrimaryKey(id);
        wmsPurchaseInputOrder.setStatus(PurchaseInputOrderStatus.WAIT_FOR_APPROVE);
        purchaseInputOrderDAO.updateByPrimaryKeySelective(wmsPurchaseInputOrder);
    }

    /**
     * 审核采购入库单
     * @param id 采购入库单id
     * @param approveResult 审核结果
     * @throws Exception
     */
    @Override
    public void approve(Long id, Integer approveResult) throws Exception {
        if(PurchaseInputOrderApproveResult.REJECTED.equals(approveResult)) {
            WmsPurchaseInputOrder wmsPurchaseInputOrder = purchaseInputOrderDAO.selectByPrimaryKey(id);
            wmsPurchaseInputOrder.setStatus(PurchaseInputOrderStatus.EDITING);
            purchaseInputOrderDAO.updateByPrimaryKeySelective(wmsPurchaseInputOrder);
            return;
        }

        if(PurchaseInputOrderApproveResult.PASSED.equals(approveResult)) {
            WmsPurchaseInputOrderDTO purchaseInputOrder = getById(id);
            PurchaseInputOrderHandler handlerChain = handlerFactory.getHandlerChain();
            handlerChain.execute(purchaseInputOrder);
        }
    }

    /**
     * 更新采购入库单状态
     * @param id 采购入库单id
     * @param status 采购入库单状态
     * @throws Exception
     */
    @Override
    public void updateStatus(Long id, Integer status) throws Exception {
        WmsPurchaseInputOrder wmsPurchaseInputOrder = purchaseInputOrderDAO.selectByPrimaryKey(id);
        wmsPurchaseInputOrder.setStatus(status);
        purchaseInputOrderDAO.updateByPrimaryKeySelective(wmsPurchaseInputOrder);
    }
    /**
     * 根据id查询采购入库单
     * @return 采购入库单
     * @throws Exception
     */
    @Override
    public WmsPurchaseInputOrderDTO getById(Long id) throws Exception {
        WmsPurchaseInputOrderDTO purchaseInputOrder = purchaseInputOrderDAO.selectByPrimaryKey(id)
                .clone(WmsPurchaseInputOrderDTO.class);

        List<WmsPurchaseInputOrderItemDTO> purchaseInputOrderItems = ObjectUtils.convertList(
                purchaseInputOrderItemDAO.listByPurchaseInputOrderId(id),
                WmsPurchaseInputOrderItemDTO.class);
        purchaseInputOrder.setItems(purchaseInputOrderItems);

        for(WmsPurchaseInputOrderItemDTO purchaseInputOrderItem : purchaseInputOrderItems) {
            List<WmsPurchaseInputOrderPutOnItemDTO> putOnItems = ObjectUtils.convertList(
                    purchaseInputOrderPutOnItemDAO.listByPurchaseInputOrderItemId(purchaseInputOrderItem.getId()),
                    WmsPurchaseInputOrderPutOnItemDTO.class);
            purchaseInputOrderItem.setPutOnItemDTOs(putOnItems);
        }

        return purchaseInputOrder;
    }

}
